import { inject, onMounted, ref } from "vue"
import { mapStore } from '@/store';

export const getEvent = () => {
    let map
    const store = mapStore()
    const count = ref(1)
    onMounted(() => {
        map = inject('$map')
    })
    const flash = () => {
        store.SceneEvent = []
        store.eventType = []
        store.event = []
        // 获取所有图层
        let layers = map.getStyle().layers
        // 获取对应id图层
        let layers_id = layers.filter(item => item.id.startsWith('event'))
        // 遍历图层
        layers_id.forEach(layer => {
            // 查询图层上的渲染要素
            let features = map.queryRenderedFeatures({
                layers: [layer.id]
            })
            // 遍历图层 打印信息
            features.forEach(feature => {
                store.SceneEvent.push(feature.properties)
                let index = store.eventType.findIndex(item => item.type === feature.properties.name);
                if (index === -1) {
                    // 如果不存在，则将新对象添加到 eventType 数组
                    store.eventType.push({
                        type: feature.properties.name,
                        typeCount: count.value
                    });
                } else {
                    store.eventType[index].typeCount++;
                }

            })
        })
    }
    return {
        flash
    }
}